package com.c.ctgproject.ui.collect

import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.BatteryInputOtherAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityBatteryInputOtherBinding
import com.c.ctgproject.entity.ResultEntity
import com.c.ctgproject.entity.guang.BatteryInpuOtherEntity
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*

/**
 *作者:LiBW
 *创建日期: 2021/9/22
 *描述:动力电池 散件信息补录 页面
 */
class BatteryInputOtherActivity : BaseActivityV2(),BatteryInputOtherAdapter.BatteryInputOtherCallBack{
    private val mBinding by lazy { ActivityBatteryInputOtherBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private var page = 1
    private var search = ""
    private val mList = ArrayList<BatteryInpuOtherEntity.DataBean.ListBean>()
    private val mAdapter by lazy { BatteryInputOtherAdapter(this, mList, listener = this) }
    override fun event() {
        getData()
        mBinding.mRecyclerview.adapter = mAdapter
        mBinding.mRecyclerview.layoutManager = LinearLayoutManager(this)
    }

    var searchMine = false
    override fun click() {
        mBinding.mTabLayout.handlerSelectReturnText {
            when (it) {

                "补录" -> {
                    searchMine = false
                    page = 1
                    getData()
                }
                "我的" -> {
                    searchMine = true
                    page = 1
                    getData()
                }
            }
        }


        mBinding.mSearchET.handlerText {
            if (it.isEmpty() || it == "") {
                search = ""
                page = 1
                getData()
            }
        }
        mBinding.mSearchTV.setOnClickListener {
            search = mBinding.mSearchET.text.handlerNull()
            page = 1
            getData()
        }
        mBinding.mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }

        mBinding.mRefresh.setOnRefreshListener {
            page = 1
            getData()
        }
    }

    private fun getData() {
        if (searchMine) {
            ApiMethods.apiSubscribe(ApiMethods.retrofit.myBatteryInputAgain(search, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("我的补录回调:$it")
                val data = getGson().fromJson(it, BatteryInpuOtherEntity::class.java).data
                data?.list?.let { list ->
                    if (page == 1) mList.clear()
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                    mBinding.mRecyclerview.handlerPosition(page)
                }
            })
        } else {
            ApiMethods.apiSubscribe(ApiMethods.retrofit.powerBatteryBulk(search, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("散件信息补录 - 列表 :$it")
                val data = getGson().fromJson(it, BatteryInpuOtherEntity::class.java).data
                data?.list?.let { list ->
                    if (page == 1) mList.clear()
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                    mBinding.mRecyclerview.handlerPosition(page)
                }
            })
        }
    }

    override fun itemDelete(position: Int, data: BatteryInpuOtherEntity.DataBean.ListBean) {
        ApiMethods.apiSubscribe(ApiMethods.retrofit.deleteInPowerBattery(data.orderId?:0), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
            MyLog.d("删除结果:$it")
            val result= getGson().fromString<ResultEntity<String>>(it)
            if(result.status==1){
                mList.removeAt(position)
                mAdapter.notifyDataSetChanged()
            }
        })
    }
}